<template>
  <div id="main" style="width: 100%; height: 100%"></div>
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "../../dashboard/mixins/resize";

export default {
  mixins: [resize],
  props: ["waterSoledMonth"],
  data() {
    return {
      chart: null,
      chartData: [],
    };
  },
  watch: {
    waterSoledMonth() {
      this.chartData = [];
      let length = 0;
      for (let i in this.waterSoledMonth) {
        length += 1;
        this.chartData.push(this.waterSoledMonth[i]);
      }
      if (length < 12) {
        for (let j = 0; j < 12 - length; j++) {
          this.chartData.push(0);
        }
      }
      this.initChart();
    },
    // chartData: {
    //   deep: true,
    //   handler(val) {
    //     this.setOptions(val);
    //   },
    // },
  },
  mounted() {
    // this.$nextTick(() => {
    //   this.initChart();
    // });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      if (!this.chart) {
        this.chart = echarts.init(this.$el, "macarons");
      }
      this.setOptions(this.chartData);
    },
    setOptions(data) {
      this.chart.setOption({
        title: {
          text: "每月供水量曲线图",
          textStyle: {
            // 其余属性默认使用全局文本样式，详见TEXTSTYLE
            // fontWeight: "bolder",
            fontSize: 12,
            color: "#ffffff",
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          axisLabel: {
            color: "#61B9C8",
            fontSize: 10,
          },
          axisLine: {
            symbol: ["none", "arrow"],
            symbolSize: [6, 6],
            symbolOffset: [0, 5],
            lineStyle: {
              color: "#122C49",
            },
          },
          axisTick: {
            color: "#122C49",
            inside: true,
          },
          z: 2,
          data: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"],
        },
        grid: {
          top: "18%",
          bottom: "5%",
          left: "5%",
          right: "5%",
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          // axisPointer: {
          //   type: "cross",
          // },
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "line", // 默认为直线，可选为：'line' | 'shadow'
          },
          backgroundColor: "#11367a",
          textStyle: {
            color: "#6dd0e3",
            fontSize: 10,
          },
        },
        yAxis: {
          type: "value",
          // interval: 50,
          // min: 0,
          // max: 400,
          // splitNumber: 7,
          axisLine: {
            symbol: ["none", "arrow"],
            symbolSize: [6, 6],
            lineStyle: {
              color: "#122C49",
            },
          },
          axisLabel: {
            color: "#61B9C8",
            showMaxLabel: false,
            fontSize: 10,
          },
          splitLine: {
            show: false,
          },
          name: "(万方)",
          nameGap: -10,
          nameTextStyle: {
            color: "#61B9C8",
            fontSize: 9,
            align: "right",
            padding: [0, 6, 0, 0],
          },
        },
        legend: {
          right: "5%",
          top: "10%",
          itemWidth: 7,
          itemHeight: 7,
          textStyle: {
            color: "#5CB1C1",
          },
        },
        series: [
          {
            type: "line",
            symbol: "none",
            smooth: true,
            data: this.chartData,
            lineStyle: {
              width: 1,
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "#FFFFFF", // 0% 处的颜色
                  },
                  {
                    offset: 0.1,
                    color: "#1E90FF", // 0% 处的颜色
                  },
                  {
                    offset: 0.5,
                    color: "#1E90FF", // 40% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "#F59111", // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              }, //背景渐变色
            },
            itemStyle: {
              color: "rgb(212,37,43)",
            },
            areaStyle: {
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "#1E90FF", // 0% 处的颜色
                  },
                  {
                    offset: 0.5,
                    color: "rgba(30,144,255,1)", // 40% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "rgba(30,144,255,0)", // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              }, //背景渐变色
              origin: "start",
            },
          },
        ],
      });
    },
  },
};
</script>
